package cn.icyzx.controller;

import cn.icyzx.models.User;
import cn.icyzx.models.Work;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import cn.icyzx.service.UserService;
import cn.icyzx.service.WorkService;
import cn.icyzx.util.WorksSelectCondition;
import cn.icyzx.util.CommonUtil;
import cn.icyzx.util.StaticConstUtil;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zk
 * @Date 2017/3/8.
 * @Email 2117251154@qq.com
 * <p>
 * numberProcess 包含了用户的一些基本的信息浏览的操作
 */
@Controller
@RequestMapping("/member")
public class NumberProcess {

    @Autowired
    WorkService workService;

    @Autowired
    UserService userService;

    /**
     * myWork 处理展示用户的作品
     *
     * @param curPage
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = "/{username}/works/{curPage}", method = RequestMethod.GET)
    public ModelAndView myWork(@PathVariable("curPage") String curPage, @PathVariable("username") String username,
                               ModelAndView modelAndView) {

        User user = userService.getUserByUserName(username);
        JSONObject jsonUser = CommonUtil.beanToJsonObject(user);
        jsonUser.putAll(workService.getOperatorWorkNumberByUid(user.getUid()));
        if (user == null)
            return modelAndView = new ModelAndView("/");

        Map<String, String> condition = new HashMap<String, String>();
        condition.put(WorksSelectCondition.UID, user.getUid());
        condition.put("curPage", curPage);

        JSONObject page = getPageByCondition(condition);

        List<Work> worksList = workService.getlistWorks(condition);
        modelAndView = new ModelAndView("personal-page");
        modelAndView.addObject("title", "创业中心-" + user.getUsername());
        modelAndView.addObject("key", "Works"); //用来区分根据不同的选择的works
        modelAndView.addObject("worksList", worksList);
        modelAndView.addObject("member_user", jsonUser);
        modelAndView.addObject("page", page);
        modelAndView.addObject(StaticConstUtil.u_works, StaticConstUtil.u_works);
        return modelAndView;
    }

    /**
     * works
     *
     * @param modelAndView 不带页码的请求，默认页码为1，转发到一般请求
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = "/{username}/works", method = RequestMethod.GET)
    public ModelAndView myWork(ModelAndView modelAndView, @PathVariable String username) {
        return new ModelAndView("forward:/member/" + username + "/works/1");
    }

    @RequestMapping(value = "/{username}", method = RequestMethod.GET)
    public ModelAndView member(ModelAndView modelAndView, @PathVariable String username) {
        return new ModelAndView("forward:/member/" + username + "/works/1");
    }

    /**
     * myComments 处理展示用户评论过的作品
     *
     * @param session
     * @param modelAndView
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = "{username}/comments/{curPage}", method = RequestMethod.GET)
    ModelAndView myComments(@PathVariable("curPage") String curPage, @PathVariable("username") String username,
                            HttpSession session, ModelAndView modelAndView) {

        User user = userService.getUserByUserName(username);
        JSONObject jsonUser = CommonUtil.beanToJsonObject(user);
        jsonUser.putAll(workService.getOperatorWorkNumberByUid(user.getUid()));
        if (user == null)
            return modelAndView = new ModelAndView("/");

        Map<String, String> condition = new HashMap<String, String>();
        condition.put(WorksSelectCondition.COMMENT_UID, user.getUid());
        condition.put("curPage", curPage);
        List<Work> worksList = workService.getlistWorks(condition);

        JSONObject page = getPageByCondition(condition);

        modelAndView = new ModelAndView("personal-page");
        modelAndView.addObject("title", "创业中心-" + user.getUsername());
        modelAndView.addObject("key", "comments"); //用来区分根据不同的选择的works
        modelAndView.addObject("worksList", worksList);
        modelAndView.addObject("member_user", jsonUser);
        modelAndView.addObject("page", page);
        modelAndView.addObject(StaticConstUtil.u_coments, StaticConstUtil.u_coments);
        return modelAndView;
    }

    /**
     * comments 不带页码的重定向到带页码请求
     *
     * @param username
     * @return java.lang.String
     */
    @RequestMapping(value = "{username}/comments", method = RequestMethod.GET)
    public String comments(@PathVariable String username) {
        return "forward:/member/" + username + "/comments/1";
    }

    /**
     * myCollections  处理展示用户收藏过的作品   我知道这样重复的太多了，等完成我改
     *
     * @param curPage
     * @param username
     * @param session
     * @param modelAndView
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = "{username}/collections/{curPage}", method = RequestMethod.GET)
    ModelAndView myCollections(@PathVariable("curPage") String curPage, @PathVariable("username") String username,
                               HttpSession session, ModelAndView modelAndView) {
        User user = userService.getUserByUserName(username);
        JSONObject jsonUser = CommonUtil.beanToJsonObject(user);
        jsonUser.putAll(workService.getOperatorWorkNumberByUid(user.getUid()));
        if (user == null)
            return modelAndView = new ModelAndView("/");

        Map<String, String> condition = new HashMap<String, String>();
        condition.put(WorksSelectCondition.COLLECTION_UID, user.getUid());
        condition.put("curPage", curPage);
        List<Work> worksList = workService.getlistWorks(condition);

        JSONObject page = getPageByCondition(condition);


        modelAndView = new ModelAndView("personal-page");
        modelAndView.addObject("title", "创业中心-" + user.getUsername());
        modelAndView.addObject("key", "collections"); //用来区分根据不同的选择的works
        modelAndView.addObject("worksList", worksList);
        modelAndView.addObject("member_user", jsonUser);
        modelAndView.addObject("page", page);
        modelAndView.addObject(StaticConstUtil.u_collections, StaticConstUtil.u_collections);
        return modelAndView;
    }

    /**
     * collections 不带页码的请求，转到带页码请求
     *
     * @param username
     * @return java.lang.String
     */
    @RequestMapping(value = "{username}/collections", method = RequestMethod.GET)
    public String collections(@PathVariable String username) {
        return "forward:/member/" + username + "/collections/1";
    }


    @RequestMapping(value = "{username}/recommends/{curPage}", method = RequestMethod.GET)
    ModelAndView myrecommends(@PathVariable("curPage") String curPage, @PathVariable("username") String username,
                              HttpSession session, ModelAndView modelAndView) {
        User user = userService.getUserByUserName(username);
        JSONObject jsonUser = CommonUtil.beanToJsonObject(user);
        jsonUser.putAll(workService.getOperatorWorkNumberByUid(user.getUid()));
        if (user == null)
            return modelAndView = new ModelAndView("/");

        Map<String, String> condition = new HashMap<String, String>();
        condition.put(WorksSelectCondition.RECOMMEND_UID, user.getUid());
        condition.put("curPage", curPage);
        List<Work> worksList = workService.getlistWorks(condition);

        JSONObject page = getPageByCondition(condition);

        modelAndView = new ModelAndView("personal-page");
        modelAndView.addObject("title", "创业中心-" + user.getUsername());
        modelAndView.addObject("key", "recommends"); //用来区分根据不同的选择的works
        modelAndView.addObject("worksList", worksList);
        modelAndView.addObject("member_user", jsonUser);
        modelAndView.addObject("page", page);
        modelAndView.addObject(StaticConstUtil.u_recommends, StaticConstUtil.u_recommends);
        return modelAndView;
    }

    /**
     * collections 不带页码的请求，转到带页码请求
     *
     * @param username
     * @return java.lang.String
     */
    @RequestMapping(value = "{username}/recommends", method = RequestMethod.GET)
    public String recommends(@PathVariable String username) {
        return "forward:/member/" + username + "/recommends/1";
    }

    /**
     * getPageByCondition 封装page属性
     *
     * @param condition
     * @return net.sf.json.JSONObject page(当前页，总页)
     */
    public JSONObject getPageByCondition(Map condition) {

        JSONObject page = new JSONObject();
        int sumPage = workService.getWorksPageSum(condition);
        page.put("sumPage", Integer.valueOf(sumPage));
        page.put("curPage", Integer.valueOf((String) condition.get("curPage")));
        page.put("condition", condition);

        return page;
    }


}